你說:
請將這張照片的八個角色摳出來,並儲存成八張照片
pythonfrom PIL import Image
# Load the uploaded image
image_path = '/mnt/data/image.png'
image = Image.open(image_path)
image.show()
已經載入圖片,接下來會進行角色的切割,並將其保存為八張獨立的圖片。請稍候。
python# Define the coordinates for cropping each character
# Assuming the image has 2 rows and 4 columns
width, height = image.size
character_width = width // 4
character_height = height // 2
cropped_images = []
for row in range(2):
for col in range(4):
left = col * character_width
upper = row * character_height
right = left + character_width
lower = upper + character_height
cropped_image = image.crop((left, upper, right, lower))
cropped_images.append(cropped_image)
# Save the cropped images
output_paths = []
for idx, cropped_img in enumerate(cropped_images):
output_path = f'/mnt/data/character_{idx + 1}.png'
cropped_img.save(output_path)
output_paths.append(output_path)
output_paths
['/mnt/data/character_1.png', '/mnt/data/character_2.png', '/mnt/data/character_3.png', '/mnt/data/character_4.png', '/mnt/data/character_5.png', '/mnt/data/character_6.png', '/mnt/data/character_7.png', '/mnt/data/character_8.png']
了解,我會使用更精確的方式來判斷角色的位置,並重新裁切這些圖片。請稍候,我將進行更細緻的分析並調整裁切範圍。
python# Display the image dimensions for precision
width, height = image.size
# Calculate precise positions based on equal grid divisions
# Each character lies in a 4x2 grid (4 columns, 2 rows)
grid_columns = 4
grid_rows = 2
# Calculate the precise width and height for each character
character_width = width / grid_columns
character_height = height / grid_rows
# Generate the exact cropping box for each character
precise_crops = []
for row in range(grid_rows):
for col in range(grid_columns):
left = int(col * character_width)
upper = int(row * character_height)
right = int(left + character_width)
lower = int(upper + character_height)
precise_crops.append((left, upper, right, lower))
# Crop each character and save with the new precise dimensions
precise_output_paths = []
for idx, box in enumerate(precise_crops):
cropped_precise_image = image.crop(box)
precise_output_path = f'/mnt/data/precise_character_{idx + 1}.png'
cropped_precise_image.save(precise_output_path)
precise_output_paths.append(precise_output_path)
precise_output_paths
['/mnt/data/precise_character_1.png', '/mnt/data/precise_character_2.png', '/mnt/data/precise_character_3.png', '/mnt/data/precise_character_4.png', '/mnt/data/precise_character_5.png', '/mnt/data/precise_character_6.png', '/mnt/data/precise_character_7.png', '/mnt/data/precise_character_8.png']
目前的環境中無法直接提供互動式框選工具,但可以透過以下方式實現:
請確認是否需要這樣的步驟,我會開始設置格線圖像!
pythonimport matplotlib.pyplot as plt
import numpy as np
# Create a grid overlay for the image
fig, ax = plt.subplots(figsize=(10, 8))
# Display the image
ax.imshow(np.array(image))
# Add grid lines
for i in range(1, 4): # 4 columns
ax.axvline(x=i * character_width, color='red', linestyle='--', linewidth=1)
for j in range(1, 2): # 2 rows
ax.axhline(y=j * character_height, color='red', linestyle='--', linewidth=1)
# Add grid labels
for row in range(2): # Rows
for col in range(4): # Columns
x = col * character_width + character_width / 2
y = row * character_height + character_height / 2
ax.text(x, y, f"{row * 4 + col + 1}", color="white", fontsize=12,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the grid image
grid_image_path = '/mnt/data/grid_overlay_image.png'
plt.savefig(grid_image_path, bbox_inches='tight')
plt.show()
grid_image_path
'/mnt/data/grid_overlay_image.png'
這是帶有格線的圖片,您可以清楚看到每個角色被分為8個區域(標示編號1到8)。請告訴我您想要裁切的區域(例如:1, 3, 5 等),或進一步提供您希望的座標或調整需求。
點此下載帶格線的圖片
python# Add a finer grid overlay to the image
fig, ax = plt.subplots(figsize=(10, 8))
# Display the image
ax.imshow(np.array(image))
# Increase grid density (8x4 instead of 4x2)
fine_grid_columns = 8
fine_grid_rows = 4
fine_character_width = width / fine_grid_columns
fine_character_height = height / fine_grid_rows
# Add finer grid lines
for i in range(1, fine_grid_columns):
ax.axvline(x=i * fine_character_width, color='blue', linestyle='--', linewidth=1)
for j in range(1, fine_grid_rows):
ax.axhline(y=j * fine_character_height, color='blue', linestyle='--', linewidth=1)
# Add finer grid labels
for row in range(fine_grid_rows): # Rows
for col in range(fine_grid_columns): # Columns
x = col * fine_character_width + fine_character_width / 2
y = row * fine_character_height + fine_character_height / 2
ax.text(x, y, f"{row * fine_grid_columns + col + 1}", color="white", fontsize=10,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the finer grid image
fine_grid_image_path = '/mnt/data/fine_grid_overlay_image.png'
plt.savefig(fine_grid_image_path, bbox_inches='tight')
plt.show()
fine_grid_image_path
'/mnt/data/fine_grid_overlay_image.png'
這是新增了更多網格的圖片,分為8列x4行,標記的格子編號從1到32。請告訴我您希望框選的格子編號或其他需求。
點此下載帶更多網格的圖片
python# Add an even finer grid overlay to the image
fig, ax = plt.subplots(figsize=(10, 8))
# Display the image
ax.imshow(np.array(image))
# Increase grid density further (16x8 grid)
extra_fine_grid_columns = 16
extra_fine_grid_rows = 8
extra_fine_character_width = width / extra_fine_grid_columns
extra_fine_character_height = height / extra_fine_grid_rows
# Add extra finer grid lines
for i in range(1, extra_fine_grid_columns):
ax.axvline(x=i * extra_fine_character_width, color='green', linestyle='--', linewidth=0.8)
for j in range(1, extra_fine_grid_rows):
ax.axhline(y=j * extra_fine_character_height, color='green', linestyle='--', linewidth=0.8)
# Add extra finer grid labels
for row in range(extra_fine_grid_rows): # Rows
for col in range(extra_fine_grid_columns): # Columns
x = col * extra_fine_character_width + extra_fine_character_width / 2
y = row * extra_fine_character_height + extra_fine_character_height / 2
ax.text(x, y, f"{row * extra_fine_grid_columns + col + 1}", color="white", fontsize=6,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the extra finer grid image
extra_fine_grid_image_path = '/mnt/data/extra_fine_grid_overlay_image.png'
plt.savefig(extra_fine_grid_image_path, bbox_inches='tight')
plt.show()
extra_fine_grid_image_path
'/mnt/data/extra_fine_grid_overlay_image.png'
這是更加細緻的網格圖片,分為16列x8行,標記的格子編號從1到128。請告訴我您希望框選的格子編號或其他具體需求。
點此下載更細緻網格的圖片
python# Add an ultra-fine grid overlay to the image
fig, ax = plt.subplots(figsize=(10, 8))
# Display the image
ax.imshow(np.array(image))
# Increase grid density even further (32x16 grid)
ultra_fine_grid_columns = 32
ultra_fine_grid_rows = 16
ultra_fine_character_width = width / ultra_fine_grid_columns
ultra_fine_character_height = height / ultra_fine_grid_rows
# Add ultra-finer grid lines
for i in range(1, ultra_fine_grid_columns):
ax.axvline(x=i * ultra_fine_character_width, color='purple', linestyle='--', linewidth=0.5)
for j in range(1, ultra_fine_grid_rows):
ax.axhline(y=j * ultra_fine_character_height, color='purple', linestyle='--', linewidth=0.5)
# Add ultra-finer grid labels
for row in range(ultra_fine_grid_rows): # Rows
for col in range(ultra_fine_grid_columns): # Columns
x = col * ultra_fine_character_width + ultra_fine_character_width / 2
y = row * ultra_fine_character_height + ultra_fine_character_height / 2
ax.text(x, y, f"{row * ultra_fine_grid_columns + col + 1}", color="white", fontsize=4,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the ultra-finer grid image
ultra_fine_grid_image_path = '/mnt/data/ultra_fine_grid_overlay_image.png'
plt.savefig(ultra_fine_grid_image_path, bbox_inches='tight')
plt.show()
ultra_fine_grid_image_path
'/mnt/data/ultra_fine_grid_overlay_image.png'
這是密度更高的網格圖片,分為32列x16行,標記的格子編號從1到512。請告訴我您希望框選的格子編號或其他具體需求。
點此下載超細緻網格的圖片
python# Define the range of grid cells to extract (131 to 318)
# Calculate their approximate positions based on grid (32x16)
columns = 32
rows = 16
# Calculate the grid cell dimensions
cell_width = width / columns
cell_height = height / rows
# Define the starting and ending grid cells (131 to 318)
start_cell = 131
end_cell = 318
# Convert grid numbers to their row and column positions
start_row = (start_cell - 1) // columns
start_col = (start_cell - 1) % columns
end_row = (end_cell - 1) // columns
end_col = (end_cell - 1) % columns
# Calculate the bounding box of the selection
left = int(start_col * cell_width)
upper = int(start_row * cell_height)
right = int((end_col + 1) * cell_width)
lower = int((end_row + 1) * cell_height)
# Add padding to expand the bounding box slightly
padding = 10 # Adjust as needed
left = max(0, left - padding)
upper = max(0, upper - padding)
right = min(width, right + padding)
lower = min(height, lower + padding)
# Crop the expanded section
expanded_crop = image.crop((left, upper, right, lower))
# Save the cropped image
expanded_crop_path = '/mnt/data/expanded_crop.png'
expanded_crop.save(expanded_crop_path)
expanded_crop_path
'/mnt/data/expanded_crop.png'
python# Further subdivide the cropped region into finer grids
# Define the size of the finer grid (e.g., 8x8)
fine_grid_columns = 8
fine_grid_rows = 8
# Get the dimensions of the cropped image
cropped_width, cropped_height = expanded_crop.size
fine_cell_width = cropped_width / fine_grid_columns
fine_cell_height = cropped_height / fine_grid_rows
# Create a finer grid overlay
fig, ax = plt.subplots(figsize=(10, 8))
# Display the cropped image
ax.imshow(np.array(expanded_crop))
# Add finer grid lines
for i in range(1, fine_grid_columns):
ax.axvline(x=i * fine_cell_width, color='orange', linestyle='--', linewidth=0.8)
for j in range(1, fine_grid_rows):
ax.axhline(y=j * fine_cell_height, color='orange', linestyle='--', linewidth=0.8)
# Add finer grid labels
for row in range(fine_grid_rows): # Rows
for col in range(fine_grid_columns): # Columns
x = col * fine_cell_width + fine_cell_width / 2
y = row * fine_cell_height + fine_cell_height / 2
ax.text(x, y, f"{row * fine_grid_columns + col + 1}", color="white", fontsize=6,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the finer grid image
fine_grid_cropped_path = '/mnt/data/fine_grid_cropped_image.png'
plt.savefig(fine_grid_cropped_path, bbox_inches='tight')
plt.show()
fine_grid_cropped_path
'/mnt/data/fine_grid_cropped_image.png'
這是擷取後的區域進一步細分為8x8的網格,標記的格子編號從1到64。請告訴我您希望框選的格子編號或其他具體需求。
點此下載細分後的網格圖片
python# Further subdivide the cropped region into even finer grids
# Define the size of the ultra-fine grid (e.g., 16x16)
ultra_fine_grid_columns = 16
ultra_fine_grid_rows = 16
# Calculate the ultra-fine cell dimensions
ultra_fine_cell_width = cropped_width / ultra_fine_grid_columns
ultra_fine_cell_height = cropped_height / ultra_fine_grid_rows
# Create an ultra-fine grid overlay
fig, ax = plt.subplots(figsize=(10, 8))
# Display the cropped image
ax.imshow(np.array(expanded_crop))
# Add ultra-fine grid lines
for i in range(1, ultra_fine_grid_columns):
ax.axvline(x=i * ultra_fine_cell_width, color='red', linestyle='--', linewidth=0.5)
for j in range(1, ultra_fine_grid_rows):
ax.axhline(y=j * ultra_fine_cell_height, color='red', linestyle='--', linewidth=0.5)
# Add ultra-fine grid labels
for row in range(ultra_fine_grid_rows): # Rows
for col in range(ultra_fine_grid_columns): # Columns
x = col * ultra_fine_cell_width + ultra_fine_cell_width / 2
y = row * ultra_fine_cell_height + ultra_fine_cell_height / 2
ax.text(x, y, f"{row * ultra_fine_grid_columns + col + 1}", color="white", fontsize=4,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the ultra-fine grid image
ultra_fine_grid_cropped_path = '/mnt/data/ultra_fine_grid_cropped_image.png'
plt.savefig(ultra_fine_grid_cropped_path, bbox_inches='tight')
plt.show()
ultra_fine_grid_cropped_path
'/mnt/data/ultra_fine_grid_cropped_image.png'
這是更加細緻的細分網格圖片,現在區域被分為16x16的網格,總計256個小格子。請告訴我您希望框選的格子編號或其他具體需求。
點此下載更細分的網格圖片
python# Define the range of ultra-fine grid cells to extract (1 to 240)
# Convert grid numbers to their row and column positions
start_cell = 1
end_cell = 240
start_row = (start_cell - 1) // ultra_fine_grid_columns
start_col = (start_cell - 1) % ultra_fine_grid_columns
end_row = (end_cell - 1) // ultra_fine_grid_columns
end_col = (end_cell - 1) % ultra_fine_grid_columns
# Calculate the bounding box of the selection within the ultra-fine grid
left = int(start_col * ultra_fine_cell_width)
upper = int(start_row * ultra_fine_cell_height)
right = int((end_col + 1) * ultra_fine_cell_width)
lower = int((end_row + 1) * ultra_fine_cell_height)
# Crop the selected section
selected_crop = expanded_crop.crop((left, upper, right, lower))
# Save the cropped image of the selected region
selected_crop_path = '/mnt/data/selected_crop.png'
selected_crop.save(selected_crop_path)
# Subdivide the cropped section into a finer grid again (e.g., 16x16)
final_fine_grid_columns = 16
final_fine_grid_rows = 16
final_fine_cell_width = (right - left) / final_fine_grid_columns
final_fine_cell_height = (lower - upper) / final_fine_grid_rows
# Create a grid overlay for the selected crop
fig, ax = plt.subplots(figsize=(10, 8))
# Display the cropped image
ax.imshow(np.array(selected_crop))
# Add final finer grid lines
for i in range(1, final_fine_grid_columns):
ax.axvline(x=i * final_fine_cell_width, color='blue', linestyle='--', linewidth=0.5)
for j in range(1, final_fine_grid_rows):
ax.axhline(y=j * final_fine_cell_height, color='blue', linestyle='--', linewidth=0.5)
# Add final finer grid labels
for row in range(final_fine_grid_rows): # Rows
for col in range(final_fine_grid_columns): # Columns
x = col * final_fine_cell_width + final_fine_cell_width / 2
y = row * final_fine_cell_height + final_fine_cell_height / 2
ax.text(x, y, f"{row * final_fine_grid_columns + col + 1}", color="white", fontsize=4,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the final finer grid image
final_fine_grid_cropped_path = '/mnt/data/final_fine_grid_cropped_image.png'
plt.savefig(final_fine_grid_cropped_path, bbox_inches='tight')
plt.show()
selected_crop_path, final_fine_grid_cropped_path
('/mnt/data/selected_crop.png', '/mnt/data/final_fine_grid_cropped_image.png')python# Further subdivide the cropped region horizontally finer (e.g., 32x16)
horizontal_fine_grid_columns = 32
horizontal_fine_grid_rows = 16 # Keeping rows the same
horizontal_fine_cell_width = (right - left) / horizontal_fine_grid_columns
horizontal_fine_cell_height = (lower - upper) / horizontal_fine_grid_rows
# Create a horizontal finer grid overlay
fig, ax = plt.subplots(figsize=(10, 8))
# Display the cropped image
ax.imshow(np.array(selected_crop))
# Add horizontal finer grid lines
for i in range(1, horizontal_fine_grid_columns):
ax.axvline(x=i * horizontal_fine_cell_width, color='green', linestyle='--', linewidth=0.5)
for j in range(1, horizontal_fine_grid_rows):
ax.axhline(y=j * horizontal_fine_cell_height, color='green', linestyle='--', linewidth=0.5)
# Add horizontal finer grid labels
for row in range(horizontal_fine_grid_rows): # Rows
for col in range(horizontal_fine_grid_columns): # Columns
x = col * horizontal_fine_cell_width + horizontal_fine_cell_width / 2
y = row * horizontal_fine_cell_height + horizontal_fine_cell_height / 2
ax.text(x, y, f"{row * horizontal_fine_grid_columns + col + 1}", color="white", fontsize=4,
ha="center", va="center", bbox=dict(facecolor="black", alpha=0.5))
# Hide axes
ax.axis('off')
# Save and display the horizontal finer grid image
horizontal_fine_grid_cropped_path = '/mnt/data/horizontal_fine_grid_cropped_image.png'
plt.savefig(horizontal_fine_grid_cropped_path, bbox_inches='tight')
plt.show()
horizontal_fine_grid_cropped_path
'/mnt/data/horizontal_fine_grid_cropped_image.png'
python# Define a function to crop images based on grid numbers
def crop_by_grid_numbers(start_cell, end_cell, grid_columns, grid_rows, cell_width, cell_height, image):
start_row = (start_cell - 1) // grid_columns
start_col = (start_cell - 1) % grid_columns
end_row = (end_cell - 1) // grid_columns
end_col = (end_cell - 1) % grid_columns
left = int(start_col * cell_width)
upper = int(start_row * cell_height)
right = int((end_col + 1) * cell_width)
lower = int((end_row + 1) * cell_height)
return image.crop((left, upper, right, lower))
# Input grid numbers for each image
grid_coordinates = [
(2, 232),
(10, 240),
(17, 247),
(25, 255),
(258, 488),
(266, 496),
(273, 503),
(281, 511)
]
# Crop images based on the given coordinates
cropped_images = []
for idx, (start, end) in enumerate(grid_coordinates):
cropped_image = crop_by_grid_numbers(
start, end,
horizontal_fine_grid_columns,
horizontal_fine_grid_rows,
horizontal_fine_cell_width,
horizontal_fine_cell_height,
selected_crop
)
output_path = f'/mnt/data/cropped_image_{idx + 1}.png'
cropped_image.save(output_path)
cropped_images.append(output_path)
cropped_images
['/mnt/data/cropped_image_1.png', '/mnt/data/cropped_image_2.png', '/mnt/data/cropped_image_3.png', '/mnt/data/cropped_image_4.png', '/mnt/data/cropped_image_5.png', '/mnt/data/cropped_image_6.png', '/mnt/data/cropped_image_7.png', '/mnt/data/cropped_image_8.png']
pythonimport zipfile
# Define the output zip file path
zip_file_path = '/mnt/data/cropped_images.zip'
# Create a zip file and add all cropped images
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
for file_path in cropped_images:
zipf.write(file_path, arcname=file_path.split('/')[-1])
zip_file_path
'/mnt/data/cropped_images.zip'